package com.auxgroup.hr.front.service.hireperson.impl;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.auxgroup.hr.front.service.hireperson.HireExpWorkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.dao.domain.PsCHireExpWork;
import com.auxgroup.hr.front.dao.domain.PsCHirePrimaryClass;
import com.auxgroup.hr.front.dao.repository.PsCHireExpWorkRepository;

/** 
 * @author lvliuhao
 * @version 创建时间：180523 
 * 入职人员工作经历service
 */
@Service("hireExpWorkService")
public class HireExpWorkServiceImpl implements HireExpWorkService {
	private static final Logger logger = LoggerFactory.getLogger(HireEducatServiceImpl.class);
	
	@Autowired
	private PsCHireExpWorkRepository PsCHireExpWorkRepository;


	@Override
	public PsCHireExpWork getHireExpWork(PsCHirePrimaryClass hirePrimary) {
		return PsCHireExpWorkRepository.findOne(hirePrimary);
	}


	@Override
	public PsCHireExpWork save(PsCHireExpWork HireExpWork) {
		if(HireExpWork.getcSeqno()==null){
			HireExpWork.setHrsRowAddOprid(HireExpWork.getRsHireId());
			HireExpWork.setHrsRowAddDttm(new Timestamp(System.currentTimeMillis()));
		}
		HireExpWork.setHrsRowUpdOprid(HireExpWork.getRsHireId());
		HireExpWork.setHrsRowUpdDttm(new Timestamp(System.currentTimeMillis()));
		Set<String> ignoreFields = new HashSet<String>(Arrays.asList("cSeqno"));
		ObjectFieldUtils.handleNullValue(HireExpWork, ignoreFields);
		return PsCHireExpWorkRepository.save(HireExpWork);
	}


	@Override
	public Boolean delete(PsCHirePrimaryClass hirePrimary) {
		try {
			PsCHireExpWorkRepository.delete(hirePrimary);
		} catch (Exception e) {
			logger.error("delete HireExpWork error: hirePrimary={}",hirePrimary,e);
			return false;
		}
		return true;
	}
	
	
	@Override
	public List<PsCHireExpWork> getAll(String rsHireId) {
		PsCHireExpWork HireExpWork = new PsCHireExpWork();
		HireExpWork.setRsHireId(rsHireId);
		return PsCHireExpWorkRepository.findAll(Example.of(HireExpWork),new Sort(Sort.Direction.DESC,"startDt"));
	}


	
	
	
}
